// pages/logins/logins.js
var app=getApp()
let category
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no:'',
    password:'',
    rpassword:'',
    name:'',
    age:'',
    sex:'',
    phone:'',
    address:'',
  },
  NiCheng(event) {
    console.log(event.detail.value)
    this.setData({
      name: event.detail.value
    })  
  },
  ZhangHao(event) {
    console.log(event.detail.value)  
    this.setData({
      no: event.detail.value
    })  
  },
  Password(event) {
    console.log(event.detail.value)  
    this.setData({
      password: event.detail.value
    })  
  },
  RPassword(event) {
    console.log(event.detail.value)
    this.setData({
      rpassword: event.detail.value
    })    
  },
  Age(event) {
    console.log(event.detail.value)  
    this.setData({
      age: event.detail.value
    })    
  },
  TongPlace(event) {
    console.log(event.detail.value)  
    this.setData({
      address: event.detail.value
    })    
  },
  LianXi(event) {
    console.log(event.detail.value)  
    this.setData({
      phone: event.detail.value
    })   
  },
  Change(e){
    // console.log(e)
    let sex =e.detail.value;
    this.setData({
      sex:sex
    })
  console.log(sex);
  },
  BuyRegister(){
     //校检各字段  
     let name  = this.data.name
     let no = this.data.no
     let age=this.data.age
     let sex= this.data.sex
     let password = this.data.password
     let rpassword = this.data.rpassword
     let address = this.data.address
     let phone = this.data.phone
     if(name.length < 2) {
       wx.showToast({
         icon: 'none',
         title: '昵称至少两位',
       })
       return
     }
     if(no.length < 6) {
       wx.showToast({
         icon: 'none',
         title: '账号至少6位',
       })
       return
     }
     if(password.length < 3) {
       wx.showToast({
         icon: 'none',
         title: '密码至少3位',
       })
       return
     }
     if(age>120){
wx.showToast({
  title: '输入年龄太大',
})
     }
     if(rpassword.length < 3) {
       wx.showToast({
         icon: 'none',
         title: '密码至少3位',
       })
       return
     }
     if(address.length < 6) {
       wx.showToast({
         icon: 'none',
         title: '通信地址至少6位',
       })
       return
     }
     if(phone.length < 11) {
       wx.showToast({
         icon: 'none',
         title: '联系人电话11位',
       })
       return
     }
    if(password == rpassword) {
      //注册买家用户的信息
    //   wx.cloud.database().collection('buyuser')
    //   .add({
    //     data:{
    //       no: no,
    //       name: name,
    //       password: password,
    //       age:age,
    //       sex:sex,
    //       phone: phone,
    //       address:address          
    //     },
    //     success(res){
    //       console.log('注册成功',res)
    //       wx.showToast({
    //         title: '注册成功',
    //       })
    //       wx.navigateTo({
    //         url: '../register/register',
    //       })
    //     }
    //   })
    // }else{
    //   wx.showToast({
    //     icon: 'none',
    //     title: '两次密码不一致',
    //   })

    wx.request({
      url: 'http://localhost:9099/user/save',
      method:"POST",
      data:{
              no: no,
              name: name,
              password: password,
              age:age,
              sex:sex,
              phone: phone,
              address:address 
      },
      success:(res)=>{
console.log(res.data);
if(res.data.code==200){
  wx.showToast({
            title: '注册成功',
          })
          wx.navigateTo({
            url: '../register/register',
          })
}
      }
    })
     }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})